package com.aruba.attendance.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.aruba.attendance.home.view.NavHome
import com.aruba.attendance.home.view.NavSettingTime
import com.aruba.attendance.home.viewmodel.HomeViewModel
import com.aruba.attendance.initial.view.InitialTimeSelect
import com.aruba.attendance.initial.viewmodel.InitialViewModel
import com.aruba.attendance.initial.viewmodel.enums.ConfigState
import com.aruba.attendance.ui.theme.AttendanceApplicationTheme
import com.aruba.attendance.ui.widget.statusbar.StatusBarSurface
import dagger.hilt.android.AndroidEntryPoint

// 导航-日历
private const val NAV_HOME = "home"

// 导航-设置-上下班时间重新选择
const val NAV_SETTING_TIME = "setting-time"

@AndroidEntryPoint
class MainActivity : FragmentActivity() {

    private val initialViewModel: InitialViewModel by lazy {
        ViewModelProvider(this)[InitialViewModel::class.java]
    }
    private val homeViewModel: HomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }

    // 拍照回调启动器
    private lateinit var activityResult: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initialViewModel.load()

        // 拍照回调启动器注册
        activityResult = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) { result ->
            if (result.resultCode == RESULT_OK) {
                // 保存打卡记录
                homeViewModel.saveRecord()
            }
        }
        setContent {
            AttendanceApplicationTheme {
                StatusBarSurface(
                    modifier = Modifier.fillMaxSize(),
                    color = if (initialViewModel.isTimeConfigured.value != ConfigState.NOT_CONFIGURED)
                        MaterialTheme.colorScheme.surface else MaterialTheme.colorScheme.background
                ) {
                    if (initialViewModel.isTimeConfigured.value != ConfigState.NOT_CONFIGURED) {// 已配置时间，则显示主界面
                        MainNav()
                    } else {// 第一次使用，则显示选择上下班时间
                        InitialTimeSelect(initialViewModel = initialViewModel)
                    }
                }
            }
        }
    }
    

    /**
     * 主UI导航
     */
    @Composable
    fun MainNav() {
        val navController = rememberNavController()

        NavHost(navController = navController, startDestination = NAV_HOME) {
            composable(route = NAV_HOME) {
                NavHome(
                    navController = navController,
                    homeViewModel = homeViewModel,
                    toStartPhoto = { callback ->
                        homeViewModel.toPhoto(this@MainActivity, activityResult) { denial ->
                            callback(denial)
                        }
                    }
                )
            }
            composable(route = NAV_SETTING_TIME) {
                NavSettingTime(
                    navController = navController,
                    initialViewModel = initialViewModel
                )
            }
        }
    }
    
}